package com.qc.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.bus.dao.BusInportDao;
import com.qc.bus.dao.BusSalesDao;
import com.qc.bus.entity.*;
import com.qc.bus.service.BusCustomerService;
import com.qc.bus.service.BusGoodsService;
import com.qc.bus.service.BusSalesService;
import com.qc.bus.vo.SalesVo;
import com.qc.sys.common.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * (BusSales)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-10 23:27:02
 */
@Service
public class BusSalesServiceImpl extends ServiceImpl<BusSalesDao, BusSales> implements BusSalesService {

    @Autowired
    private BusSalesDao salesDao;

    @Autowired
    private BusCustomerService customerService;

    @Autowired
    private BusGoodsService goodsService;

    @Override
    public DataGridView queryAllSales(SalesVo salesVo) {
        IPage<BusSales> page = new Page<>(salesVo.getPage(),salesVo.getLimit());
        QueryWrapper<BusSales> qw = new QueryWrapper<>();
        qw.eq(salesVo.getGoodsid()!=null,"goodsid",salesVo.getGoodsid());
        qw.eq(salesVo.getCustomerid()!=null,"customerid",salesVo.getCustomerid());
        qw.ge(salesVo.getStartTime()!=null,"salestime",salesVo.getStartTime());
        qw.le(salesVo.getEndTime()!=null,"salestime",salesVo.getEndTime());
        this.salesDao.selectPage(page,qw);
        List<BusSales> records = page.getRecords();
        for (BusSales sales : records) {
            if(null!=sales.getCustomerid()){
                BusCustomer customer = this.customerService.getById(sales.getCustomerid());
                sales.setCustomername(customer.getCustomername());
            }
            if(null!=sales.getGoodsid()) {
                BusGoods goods = this.goodsService.getById(sales.getGoodsid());
                sales.setGoodsname(goods.getGoodsname());
                sales.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public BusSales InsertSales(BusSales sales) {
        this.salesDao.insert(sales);
        //更新销售信息
        BusGoods goods = this.goodsService.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()-sales.getNumber());
        this.goodsService.UpdateGoods(goods);
        return sales;
    }

    @Override
    public BusSales UpdateSales(BusSales sales) {
        //用之前的进货
        BusSales oldObj = this.salesDao.selectById(sales.getId());
        //根据商品id查询
        BusGoods goods = this.goodsService.getById(oldObj.getGoodsid());
        //1000 + oldObj 100 =  1100 goods 50 1050
        goods.setNumber(goods.getNumber()+oldObj.getNumber()-sales.getNumber());

        this.goodsService.UpdateGoods(goods);
        this.salesDao.updateById(sales);
        return null;
    }

    @Override
    public boolean removeById(Serializable id) {
        BusSales sales = this.salesDao.selectById(id);
        BusGoods goods = this.goodsService.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()+sales.getNumber());
        this.goodsService.UpdateGoods(goods);
        return super.removeById(id);
    }

}